/*!
 * \file frservice_get.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include <frdb_cursor.h>

#include "frct_expect.h"
#include "frservice_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

ApmContext *p_frame_ref = NULL;
pthread_spinlock_t st_thread;
static void __node_free(Frlist_node *p_node)
{
    Host_service *p_st_service=NULL;

    if(p_node && p_node->data)
    {
        p_st_service = (Host_service *)(p_node->data);
        frfree(p_st_service->p_data);
        frfree(p_st_service);

    }
    return;
}

static void __case1(void)
{
    int i_l7_proto = 80;
    u_int ui_host_ip = 19216811;
    u_short us_host_port = 80;
    char *p_data = (char *)frstrdup ((char *)"HTTP");
    unsigned int ui_data_size = 5;
    char *p_out = (char *)frmalloc (5);
    int i_ret = 0;

    p_frame_ref = (ApmContext *)frmalloc (sizeof (ApmContext));
    memset (p_frame_ref, 0x00, sizeof (ApmContext));

    Frservice_ref *p_st_service_ref = (Frservice_ref *)frmalloc (sizeof (Frservice_ref));

    p_st_service_ref->p_hash_hdr = frhash2_create2 (1024);
    p_st_service_ref->p_splock = &st_thread;
    pthread_spin_init(p_st_service_ref->p_splock, 0);

    p_frame_ref->p_st_service_ref = p_st_service_ref;

    frservice_add(p_frame_ref, i_l7_proto, ui_host_ip, us_host_port
            , p_data, ui_data_size);

    frservice_get(p_frame_ref, i_l7_proto, ui_host_ip, us_host_port, p_out, ui_data_size, &i_ret);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_out, (char *)"HTTP");

    frfree (p_data);
    frfree (p_out);

    return;

}

static void __case2(void)
{
    int i_l7_proto = 80;
    u_int ui_host_ip = 19216811;
    u_short us_host_port = 80;
    char *p_data = (char *)frstrdup ((char *)"test");
    unsigned int ui_data_size = 5;
    char *p_out = (char *)frmalloc (5);
    int i_ret = 0;

    frservice_add(p_frame_ref, i_l7_proto, ui_host_ip, us_host_port
            , p_data, ui_data_size);

    frservice_get(p_frame_ref, i_l7_proto, ui_host_ip, us_host_port, p_out, ui_data_size, &i_ret);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_out, (char *)"test");

    frfree (p_data);
    frfree (p_out);
    frhash2_free2(p_frame_ref->p_st_service_ref->p_hash_hdr, __node_free);
    pthread_spin_init(p_frame_ref->p_st_service_ref->p_splock, 0);
    pthread_spin_destroy (p_frame_ref->p_st_service_ref->p_splock);
    frfree (p_frame_ref->p_st_service_ref);
    frfree (p_frame_ref);
    return;

}

static void __case3(void)
{
    int i_l7_proto = 80;
    u_int ui_host_ip = 19216811;
    u_short us_host_port = 80;
    char *p_data = (char *)frstrdup ((char *)"test");
    unsigned int ui_data_size = 5;
    char *p_out = (char *)frmalloc (5);
    int i_ret = 0;

    memset (p_out, 0x00, 5);
    frservice_add(NULL, i_l7_proto, ui_host_ip, us_host_port
            , p_data, ui_data_size);

    frservice_get(NULL, i_l7_proto, ui_host_ip, us_host_port, p_out, ui_data_size, &i_ret);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_out,(char *)"");

    frfree (p_data);
    frfree (p_out);

    return;

}


int add_suite_frservice_get(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frservice_get frservice_get()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);

    return FR_SUCC;
}
